<template>
    <div class="views-fuwuxiangmu-add">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加服务项目 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" :label-width="labelWidth" status-icon validate-on-rule-change>
                    <el-form-item label="项目编号" prop="xiangmubianhao" :rules="[{required:true, message:'请填写项目编号'}]">
                        <el-input type="text" placeholder="输入项目编号" style="width: 450px" v-model="form.xiangmubianhao" />
                    </el-form-item>

                    <el-form-item label="项目名称" prop="xiangmumingcheng" required :rules="[{required:true, message:'请填写项目名称'}]">
                        <el-input type="text" placeholder="输入项目名称" style="width: 450px" v-model="form.xiangmumingcheng" />
                    </el-form-item>

                    <el-form-item label="项目分类" prop="xiangmufenlei" required :rules="[{required:true, message:'请填写项目分类'}]">
                        <el-select v-model="form.xiangmufenlei"><e-select-option type="option" module="fuwufenlei" value="id" label="fenleimingcheng"></e-select-option></el-select>
                    </el-form-item>

                    <el-form-item label="项目图片" prop="xiangmutupian"> <e-upload-image v-model="form.xiangmutupian" is-paste></e-upload-image> </el-form-item>

                    <el-form-item label="价格" prop="jiage" required :rules="[{required:true, message:'请填写价格'}, {validator:rule.checkNumber, message:'输入一个有效数字'}]">
                        <el-input type="number" placeholder="输入价格" style="width: 450px" v-model.number="form.jiage" />
                    </el-form-item>

                    <el-form-item label="服务时间" prop="fuwushijian">
                        <el-input type="text" placeholder="输入服务时间" style="width: 450px" v-model="form.fuwushijian" />
                    </el-form-item>

                    <el-form-item label="服务介绍" prop="fuwujieshao"> <e-editor v-model="form.fuwujieshao" @getContent="getfuwujieshaoContent"></e-editor> </el-form-item>

                    <el-form-item label="发布人" prop="faburen"> <el-input v-model="form.faburen" readonly style="width: 250px"></el-input> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";
    import EEditor from "@/components/EEditor.vue";

    import { ref, reactive, computed } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useFuwuxiangmuCreateForm, canFuwuxiangmuInsert } from "@/module";
    import { extend } from "vue-design-plugin";

    const route = useRoute();
    const props = defineProps({
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
        labelWidth: {
            type: String,
            default: "140px",
        },
    });
    const { form } = useFuwuxiangmuCreateForm();
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);
    var submit = () => {
        return new Promise((resolve, reject) => {
            formModel.value
                .validate()
                .then((res) => {
                    if (loading.value) return;
                    loading.value = true;
                    canFuwuxiangmuInsert(form).then(
                        (res) => {
                            loading.value = false;
                            if (res.code == 0) {
                                emit("success", res.data);
                                if (props.isHouxu) {
                                    ElMessage.success("添加成功");
                                    formModel.value.resetFields();
                                }
                            } else {
                                ElMessageBox.alert(res.msg);
                            }
                            resolve(res);
                        },
                        (err) => {
                            loading.value = false;
                            ElMessageBox.alert(err.message);
                            reject(err);
                        }
                    );
                })
                .catch((err) => {
                    reject(err);
                });
        });
    };

    const getfuwujieshaoContent = (v) => {
        form.fuwujieshao = v;
    };
</script>

<style scoped lang="scss">
    .views-fuwuxiangmu-add {
    }
</style>
